package multiplayerShooter;

import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import imageLoader.ImageLoader;

import javax.swing.JFrame;

import multiplayerShooter.WrapperClasses.WindowListenerWrapper;

public class MultiplayerShooter implements Printer {

	/**
	 * 
	 */
	private static final int height = 768;
	private static final int width = 1024;
	
	private static final long serialVersionUID = 1L;
	private VolatileImage offscreen;
	private Graphics bufferGraphics;
	private Frame frame = new JFrame();
	
	private Image background;
	private Thread paintThread;
	private ArrayList<Block> blocks = new ArrayList<Block>();
	private ArrayList<Player> players = new ArrayList<Player>();//Generally only two -human- players 
														//Computer Opponents Coming Soon
	
	
	private ArrayList<Printable> printableObjects = new ArrayList<Printable>();
	private ArrayList<Integer> removeObjects = new ArrayList<Integer>();

	public static void main(String args[]) {
		ImageLoader.setImagePath("images/");
		MultiplayerShooter app = new MultiplayerShooter();
		app.init();
	}

	private void init() {
		
		
		new Player(this,players,blocks);
		new Player(this,players,blocks);		
		PlayerInputHandler pih1 = new PlayerInputHandler(players.get(0));
		pih1.setKeys(KeyEvent.VK_UP,KeyEvent.VK_DOWN,KeyEvent.VK_LEFT,KeyEvent.VK_RIGHT,KeyEvent.VK_L);
		
		PlayerInputHandler pih2= new PlayerInputHandler(players.get(1));
		pih2.setKeys(KeyEvent.VK_W,KeyEvent.VK_S,KeyEvent.VK_A,KeyEvent.VK_D,KeyEvent.VK_Q);

		initFrame();
		createMap();

		

		this.paintThread = new Thread(){public void run() {paintThread();}};
		this.paintThread.setName("Painting Thread");

		this.background = ImageLoader.getImage("Level1.gif");		
		this.frame.addKeyListener(pih1);
		this.frame.addKeyListener(pih2);		
		printableObjects.addAll(blocks);
		printableObjects.addAll(players);
		this.paintThread.start();

	}
	private void initFrame(){
		this.frame.setUndecorated(false);
		this.frame.setIgnoreRepaint(true);
		this.frame.setResizable(false);
		this.frame.setSize(width, height);
		this.frame.setVisible(true);
		this.frame.addWindowListener(
			new WindowListenerWrapper() {
				@Override
				public void windowClosing(WindowEvent e) {System.exit(0);}
			}
			);
		this.offscreen = this.frame.createVolatileImage(width,height);
		this.bufferGraphics = this.offscreen.getGraphics();
		
	}

	
	private void createMap(){
		this.blocks.add(new Block(100, 580));
		this.blocks.add(new Block(180, 500));
		this.blocks.add(new Block(240, 420));
		this.blocks.add(new MovingBlock(200, 500, 340, 340));
		this.blocks.add(new Block(240, 240));
		this.blocks.add(new Block(800, 580));
		this.blocks.add(new Block(750, 500));
		this.blocks.add(new MovingBlock(720, 900, 420, 420));
		this.blocks.add(new Block(800, 320));
		this.blocks.add(new MovingBlock(500, 900, 240, 240));
		this.blocks.add(new MovingBlock(475, 475, 300, 550));
	}
	

	

	private void paintThread() {
		Graphics graph = this.frame.getGraphics();
		while (this.frame.isVisible()) {

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
			}

			//gameScreen(graph);
			paintScreen(graph);
		}
	}
	
	private void paintScreen(Graphics g)
	{
		
		bufferGraphics.drawImage(this.background, 0, 0, this.frame);
		
		int index = 0;
		while(index<printableObjects.size()){
			Printable current = printableObjects.get(index);
			if(current.printItem())
				current.draw(bufferGraphics);
			else
				removeObjects.add(index);
			index++;
		}
		g.drawImage(this.offscreen, 0, 0, this.frame);
		if(!removeObjects.isEmpty())
			for(Iterator<Integer> it = removeObjects.listIterator();it.hasNext();){
				printableObjects.remove(it.next());
				it.remove();
			}
	}
	

	public void addPrintableObject(Printable obj) {
		printableObjects.add(obj);		
	}

	public int getMaxX() {
		return width;
	}

	public int getMinX() {
		return 0;
	}
	

}
